import logging
import os
from logging.handlers import RotatingFileHandler

class Logger:
    def __init__(self, name, log_file, level=logging.DEBUG):
        current_path = os.path.dirname(os.path.abspath(__file__))
        parent_path = os.path.dirname(current_path)
        suite_path = parent_path + "\log"

        self.log_file = os.path.join(suite_path, log_file)

        self.logger = logging.getLogger(name)
        self.logger.setLevel(level)
        # 创建日志格式
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        # 创建文件处理器
        file_handler = RotatingFileHandler(self.log_file, maxBytes=1048576, backupCount=3, encoding='utf-8')
        file_handler.setFormatter(formatter) # 设置格式
        self.logger.addHandler(file_handler) # 添加到对应的处理器中
        # 创建控制台处理器
        console_handler = logging.StreamHandler()
        console_handler.setFormatter(formatter)
        self.logger.addHandler(console_handler)


